package com.joysuch.wwyt.mh.websocket;

import cn.zlg.common.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationAlarmCountBean;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationPageAlarmCertificateOrAreaBean;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationScreenAreaPersonBean;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationScreenBinderBean;
import com.joysuch.wwyt.bp.entity.risk.BpCompany;
import com.joysuch.wwyt.bp.entity.vo.AlarmMessageVO;
import com.joysuch.wwyt.mh.bean.*;
import com.joysuch.wwyt.mh.dto.CustomizeMonitorBigScreenPointDto;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author ZhaoQiuWei
 * @Date 2021/11/11 10:49
 */
@Data
@Slf4j
public class WebSocketCovertDataFactory {
    private Integer flag;
    private List list;

    public WebSocketCovertDataFactory(Integer flag, List list) {
        this.flag = flag;
        this.list = list;
    }

    /**
     * 监测点实时数据
     */
    private void pointRealTimeData() {
        List<MonitorPointWebSocketData> pointDataList = (List<MonitorPointWebSocketData>) list;
        Map<String, List<MonitorPointWebSocketData>> pointDataMap = pointDataList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.MONITOR.getSubType())));
        for (String key : pointDataMap.keySet()) {
            List<MonitorPointWebSocketData> dataList = pointDataMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.MONITOR_POINT_REAL_TIME_DATA.getBusEnType(), dataList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 危险源报警
     */
    private void pointAlarm() {
        List<MonitorWebSocketPointAlarmData> pointAlarmList = (List<MonitorWebSocketPointAlarmData>) list;
        Map<String, List<MonitorWebSocketPointAlarmData>> pointAlarmMap = pointAlarmList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.MONITOR.getSubType())));
        for (String key : pointAlarmMap.keySet()) {
            List<MonitorWebSocketPointAlarmData> dataList = pointAlarmMap.get(key);
            String flagType = dataList.get(0).getFlagType();
            String busType = null;
            if (flagType.equals("ALL")) {
                busType = MonitorPointBusType.MONITOR_POINT_ALARM_MESSAGE_ALL.getBusEnType();
            } else if (flagType.equals("ADD")) {
                busType = MonitorPointBusType.MONITOR_POINT_ALARM_MESSAGE_ADD.getBusEnType();
            } else if (flagType.equals("HANDLE")) {
                busType = MonitorPointBusType.MONITOR_POINT_ALARM_MESSAGE_HANDLE.getBusEnType();
            } else if (flagType.equals("HANDLE_ALL")) {
                busType = MonitorPointBusType.MONITOR_POINT_ALARM_MESSAGE_HANDLE_ALL.getBusEnType();
            }
            MonitorWebsocketDto dto = new MonitorWebsocketDto(busType, dataList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 风险研判-承诺公告
     */
    private void riskCompany() {
        List<BpCompany> riskCompanyList = (List<BpCompany>) list;
        Map<String, List<BpCompany>> riskCompanyMap = riskCompanyList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.RISK_COMPANY.getSubType())));
        for (String key : riskCompanyMap.keySet()) {
            List<BpCompany> dataList = riskCompanyMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.RISK_COMPANY_BIG_SCREEN_DATA.getBusEnType(), dataList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 区域报警
     */
    private void districtAlarm() {
        List<MonitorWebSocketDistrictAlarmData> districtAlarmList = (List<MonitorWebSocketDistrictAlarmData>) list;
        Map<String, List<MonitorWebSocketDistrictAlarmData>> districtAlarmMap = districtAlarmList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.RISK_DISTRICT.getSubType())));
        for (String key : districtAlarmMap.keySet()) {
            List<MonitorWebSocketDistrictAlarmData> dataList = districtAlarmMap.get(key);
            String flagType = dataList.get(0).getFlagType();
            String busType = null;
            if (flagType.equals("ALL")) {
                busType = MonitorPointBusType.RISK_DISTRICT_ALARM_MESSAGE_ALL.getBusEnType();
            } else if (flagType.equals("ADD")) {
                busType = MonitorPointBusType.RISK_DISTRICT_ALARM_MESSAGE_ADD.getBusEnType();
            } else if (flagType.equals("HANDLE")) {
                busType = MonitorPointBusType.RISK_DISTRICT_ALARM_MESSAGE_HANDLE.getBusEnType();
            } else if (flagType.equals("HANDLE_ALL")) {
                busType = MonitorPointBusType.RISK_DISTRICT_ALARM_MESSAGE_HANDLE_ALL.getBusEnType();
            }
            MonitorWebsocketDto dto = new MonitorWebsocketDto(busType, dataList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 巡检排查报警
     */
    private void locateInspectAlaem() {
        List<MonitorWebSocketInspectAlarmData> areaAlarmList = (List<MonitorWebSocketInspectAlarmData>) list;
        if (CollectionUtils.isNotEmpty(areaAlarmList)) {
            Map<String, List<MonitorWebSocketInspectAlarmData>> inspectAlarm = areaAlarmList.stream().collect(Collectors.groupingBy(r ->
                    getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.INVESTIGATION.getSubType())));
            for (String key : inspectAlarm.keySet()) {
                List<MonitorWebSocketInspectAlarmData> inspectList = inspectAlarm.get(key);
                String flagType = inspectList.get(0).getFlagType();
                String busType = null;
                switch (flagType) {
                    case "ALL":
                        busType = MonitorPointBusType.LOCATE_SCREEN_INSPECT_ALARM_ALL.getBusEnType();
                        break;
                    case "ADD":
                        busType = MonitorPointBusType.LOCATE_SCREEN_INSPECT_ALARM_ADD.getBusEnType();
                        break;
                    case "HANDLE":
                        busType = MonitorPointBusType.LOCATE_SCREEN_INSPECT_ALARM_HANDLE.getBusEnType();
                        break;
                    case "HANDLE_ALL":
                        busType = MonitorPointBusType.LOCATE_SCREEN_INSPECT_ALARM_HANDLE_ALL.getBusEnType();
                        break;
                }
                MonitorWebsocketDto dto = new MonitorWebsocketDto(busType, inspectList);
                MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
            }
        }
    }


    /**
     * AI报警
     */
    private void aIAlarm() {
        List<AlarmMessageVO> aIAlarmList = (List<AlarmMessageVO>) list;
        if (CollectionUtils.isNotEmpty(aIAlarmList)) {
            Map<String, List<AlarmMessageVO>> aIAlarm = aIAlarmList.stream().collect(Collectors.groupingBy(r ->
                    getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.AI_ALARM.getSubType())));
            for (String key : aIAlarm.keySet()) {
                List<AlarmMessageVO> aiAlarmList = aIAlarm.get(key);
                String flagType = aiAlarmList.get(0).getFlagType();
                String busType = null;
                switch (flagType) {
                    case "ALL":
                        busType = MonitorPointBusType.AI_ALARM_ALL.getBusEnType();
                        break;
                    case "ADD":
                        busType = MonitorPointBusType.AI_ALARM_ADD.getBusEnType();
                        break;
                    case "HANDLE":
                        busType = MonitorPointBusType.AI_ALARM_HANDLE.getBusEnType();
                        break;
                    case "HANDLE_ALL":
                        busType = MonitorPointBusType.AI_ALARM_HANDLE_ALL.getBusEnType();
                        break;
                }
                MonitorWebsocketDto dto = new MonitorWebsocketDto(busType, aiAlarmList);
                MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
            }
        }
    }

    /**
     * 自定义监测点
     */
    private void customizeMonitor() {
        List<CustomizeMonitorBigScreenPointDto> customizeMonitorList = (List<CustomizeMonitorBigScreenPointDto>) list;
        Map<String, List<CustomizeMonitorBigScreenPointDto>> customizeMonitorMap = customizeMonitorList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.CUSTOMIZE_MONITOR.getSubType())));
        for (String key : customizeMonitorMap.keySet()) {
            List<CustomizeMonitorBigScreenPointDto> dataList = customizeMonitorMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.CUSTOMIZE_MONITOR_POINT_DATA.getBusEnType(), dataList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 大屏人车管控-人员列表
     */
    private void locateBinder() {
        List<OrientationScreenBinderBean> binderList = (List<OrientationScreenBinderBean>) list;
        Map<String, List<OrientationScreenBinderBean>> binderMap = binderList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.LOCATE_SCREEN.getSubType())));
        for (String key : binderMap.keySet()) {
            List<OrientationScreenBinderBean> screenBinderList = binderMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_BINDERS.getBusEnType(), screenBinderList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 大屏人车管控-人员的区域列表
     */
    private void locateAreaPerson() {
        List<OrientationScreenAreaPersonBean> areaPersonList = (List<OrientationScreenAreaPersonBean>) list;
        Map<String, List<OrientationScreenAreaPersonBean>> areaPersonMap = areaPersonList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.LOCATE_SCREEN.getSubType())));
        for (String key : areaPersonMap.keySet()) {
            List<OrientationScreenAreaPersonBean> screenBinderList = areaPersonMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_AREA_PERSON.getBusEnType(), screenBinderList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 作业报警
     */
    private void locateCertificateAlarm() {
        List<OrientationPageAlarmCertificateOrAreaBean> certificateAlarmList = (List<OrientationPageAlarmCertificateOrAreaBean>) list;
        Map<String, List<OrientationPageAlarmCertificateOrAreaBean>> certificateAlarmMap = certificateAlarmList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.LOCATE_SCREEN.getSubType())));
        for (String key : certificateAlarmMap.keySet()) {
            List<OrientationPageAlarmCertificateOrAreaBean> certificateList = certificateAlarmMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_CERTIFICATE_ALARM.getBusEnType(), certificateList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 区域报警
     */
    private void locateAreaAlarm() {
        List<OrientationPageAlarmCertificateOrAreaBean> areaAlarmList = (List<OrientationPageAlarmCertificateOrAreaBean>) list;
        Map<String, List<OrientationPageAlarmCertificateOrAreaBean>> areaAlarmMap = areaAlarmList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.LOCATE_SCREEN.getSubType())));
        for (String key : areaAlarmMap.keySet()) {
            List<OrientationPageAlarmCertificateOrAreaBean> areaList = areaAlarmMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_AREA_ALARM.getBusEnType(), areaList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 车辆报警
     */
    private void locateCarAlarm() {
        List<OrientationPageAlarmCertificateOrAreaBean> areaAlarmList = (List<OrientationPageAlarmCertificateOrAreaBean>) list;
        Map<String, List<OrientationPageAlarmCertificateOrAreaBean>> areaAlarmMap = areaAlarmList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.LOCATE_SCREEN.getSubType())));
        for (String key : areaAlarmMap.keySet()) {
            List<OrientationPageAlarmCertificateOrAreaBean> areaList = areaAlarmMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_CAR_ALARM.getBusEnType(), areaList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    /**
     * 报警总数
     */
    private void locateAlarmCount() {
        List<OrientationAlarmCountBean> alarmCountList = (List<OrientationAlarmCountBean>) list;
        Map<String, List<OrientationAlarmCountBean>> alarmCountMap = alarmCountList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.LOCATE_SCREEN.getSubType())));
        for (String key : alarmCountMap.keySet()) {
            List<OrientationAlarmCountBean> areaList = alarmCountMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.LOCATE_SCREEN_ALARM_COUNT.getBusEnType(), areaList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    private void districtList() {
        List<RiskDistrictScreenListBean> dataList = (List<RiskDistrictScreenListBean>) list;
        Map<String, List<RiskDistrictScreenListBean>> beanMap = dataList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.RISK_DISTRICT_LIST.getSubType())));
        for (String key : beanMap.keySet()) {
            List<RiskDistrictScreenListBean> beanList = beanMap.get(key);
            String flagType = beanList.get(0).getFlagType();
            String busType = null;
            switch (flagType) {
                case "ALL":
                    busType = MonitorPointBusType.RISK_DISTRICT_LIST_ALL.getBusEnType();
                    break;
                case "ADD":
                    busType = MonitorPointBusType.RISK_DISTRICT_LIST_ADD.getBusEnType();
                    break;
                case "UPDATE":
                    busType = MonitorPointBusType.RISK_DISTRICT_LIST_UPDATE.getBusEnType();
                    break;
                case "HANDLE":
                    busType = MonitorPointBusType.RISK_DISTRICT_LIST_HANDLE.getBusEnType();
                    break;
            }
            MonitorWebsocketDto dto = new MonitorWebsocketDto(busType, beanList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }


    private void riskDistrictMap() {
        List<RiskDistrictMapBean> districtMapBeanList = (List<RiskDistrictMapBean>) list;
        Map<String, List<RiskDistrictMapBean>> diatrictMap = districtMapBeanList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.RISK_DISTRICT_LIST.getSubType())));
        for (String key : diatrictMap.keySet()) {
            List<RiskDistrictMapBean> areaList = diatrictMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.RISK_DISTRICT_MAP.getBusEnType(), areaList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }


    private void riskPointCount() {
        List<RiskPointCountBean> dataList = (List<RiskPointCountBean>) list;
        Map<String, List<RiskPointCountBean>> beanMap = dataList.stream().collect(Collectors.groupingBy(r ->
                getKey(r.getTenentId(), r.getOrgCode().substring(0, 7), WebsocketSubType.RISK_DISTRICT_LIST.getSubType())));
        for (String key : beanMap.keySet()) {
            List<RiskPointCountBean> beanList = beanMap.get(key);
            MonitorWebsocketDto dto = new MonitorWebsocketDto(MonitorPointBusType.RISK_POINT_COUNT.getBusEnType(), beanList);
            MonitorPointDataWebSocket.sendData(key, JSON.toJSONString(dto));
        }
    }

    private String getKey(Long tenentId, String orgCode, String subType) {
        return tenentId + "_" + orgCode + "_" + subType;
    }

    public void covertData(Integer flag) {
        if (flag.equals(WebsocketDataType.POINT_REALTIME_DATA.getIndex())) {
            pointRealTimeData();
        } else if (flag.equals(WebsocketDataType.POINT_ALARM.getIndex())) {
            pointAlarm();
        } else if (flag.equals(WebsocketDataType.RISK_COMPANY.getIndex())) {
            riskCompany();
        } else if (flag.equals(WebsocketDataType.DISTRICT_ALARM.getIndex())) {
            districtAlarm();
        } else if (flag.equals(WebsocketDataType.CUSTOMIZE_MONITOR.getIndex())) {
            customizeMonitor();
        } else if (flag.equals(WebsocketDataType.LOCATE_BINDER.getIndex())) {
            locateBinder();
        } else if (flag.equals(WebsocketDataType.LOCATE_AREA_PERSON.getIndex())) {
            locateAreaPerson();
        } else if (flag.equals(WebsocketDataType.LOCATE_CERTIFICATE_ALARM.getIndex())) {
            locateCertificateAlarm();
        } else if (flag.equals(WebsocketDataType.LOCATE_AREA_ALRAM.getIndex())) {
            locateAreaAlarm();
        } else if (flag.equals(WebsocketDataType.LOCATE_ALRAM_COUNT.getIndex())) {
            locateAlarmCount();
        } else if (flag.equals(WebsocketDataType.INSPECT_ALARM.getIndex())) {
            locateInspectAlaem();
        } else if (flag.equals(WebsocketDataType.AI_ALARM.getIndex())) {
            aIAlarm();
        } else if (flag.equals(WebsocketDataType.RISK_DISTRICT_LIST.getIndex())) {
            districtList();
        } else if (flag.equals(WebsocketDataType.RISK_POINT_COUNT.getIndex())) {
            riskPointCount();
        } else if (flag.equals(WebsocketDataType.RISK_DISTRICT_MAP.getIndex())) {
            riskDistrictMap();
        } else if (flag.equals(WebsocketDataType.LOCATE_CAR_ALARM.getIndex())) {
            locateCarAlarm();
        }
    }

}
